package com.lianniu.web.tea.user.info.service.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.lianniu.web.base.BaseResult;
import com.lianniu.web.tea.order.model.TeaOrder;
import com.lianniu.web.tea.pay.service.impl.TeaPayServiceImpl;
import com.lianniu.web.tea.user.addr.dao.TeaUserAddrDao;
import com.lianniu.web.tea.user.addr.model.TeaUserAddr;
import com.lianniu.web.tea.user.info.dao.TeaFrontusersDao;
import com.lianniu.web.tea.user.info.model.TeaFrontusers;
import com.lianniu.web.tea.user.info.service.ITeaInfoService;
import com.lianniu.web.util.TeaContant;

@Service
@Transactional
public class TeaInfoServiceImpl implements ITeaInfoService {
	
	private final static Logger logger = Logger.getLogger(TeaInfoServiceImpl.class);
	
	@Autowired
	private TeaFrontusersDao teaFrontusersDao;
	
	@Autowired
	private TeaUserAddrDao teaUserAddrDao;

	@Override
	public BaseResult getTeaInfo(String userId) {
		logger.info("TeaPayServiceImpl balancePay start");
		TeaFrontusers frontusers = teaFrontusersDao.selectByPrimaryKey(Long.valueOf(userId));
		TeaUserAddr defaultAddr = teaUserAddrDao.selectDefaultAddr(frontusers.getUsersid().toString());
		frontusers.setTeaUserAddr(defaultAddr);
		return BaseResult.success(frontusers);
	}
	
	@Override
	public TeaFrontusers getTeaInfoByUserId(String userId) {
		logger.info("TeaPayServiceImpl balancePay start");
		TeaFrontusers frontusers = teaFrontusersDao.selectByPrimaryKey(Long.valueOf(userId));
		return frontusers;
	}

	@Override
	public BaseResult updateWeixinForuser(TeaFrontusers teaFrontusers) {
		teaFrontusersDao.updateByPrimaryKeySelective(teaFrontusers);
		return BaseResult.success("OK");
	}

	@Override
	public TeaFrontusers loadByToken(String token) {
		List<TeaFrontusers> teaFrontusers = teaFrontusersDao.getUserList(token);
		if (!CollectionUtils.isEmpty(teaFrontusers)) {
			return teaFrontusers.get(0);
		}
		return null;
	}
}
